package com.hzpd.hainan.module.news.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hzpd.hainan.R;
import com.hzpd.hainan.app.ListBaseAdapter;
import com.hzpd.hainan.model.news.NewsChannelBean;

import java.util.Collections;
import java.util.List;

public class DragAdapter extends ListBaseAdapter<NewsChannelBean> {
	private Activity activity;
	private List<NewsChannelBean> resetList1;
	private List<NewsChannelBean> resetList2;
	private int mHidePosition = -1;
	private int type;

	public DragAdapter(Activity activity, List<NewsChannelBean> list, int type) {
		super(activity);
		this.activity = activity;
		this.list = list;
		this.type = type;

		//reset();

	}

	public void setList(List<NewsChannelBean> titleData) {
		this.list = titleData;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public void appendData(List<NewsChannelBean> data, boolean isClearOld) {
		super.appendData(data, isClearOld);
		//getChoiceList();
	}

	boolean animFlag = false;

	public void setAnim(boolean animFlag) {
		this.animFlag = animFlag;
	}

	public void deleteDataAtPosition(int position){
		list.remove(position);
		notifyDataSetChanged();
	}
	public void addDataAtPosition(NewsChannelBean ncb,int position){
		if (position == -1){
			list.add(ncb);
		}else{
			list.add(position,ncb);
		}
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int p = position;
		final NewsChannelBean ncb = list.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.news_channel_item, parent, false);
		}
		final TextView tv = (TextView) convertView.findViewById(R.id.news_cancel_griditem_tv);

		tv.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.pop_grid_textview_shape));
		tv.setTextColor(activity.getResources().getColor(R.color.colorPrimary));
		if (p == list.size() - 1 && animFlag) {
			convertView.setVisibility(View.INVISIBLE);
		} else {
			convertView.setVisibility(View.VISIBLE);
		}
		/*
		if (type == 0) {
			// 隐藏推荐选项按钮（始终处于选中状态）
			if ("0".equals(ncb.getTid())) {
				convertView.setVisibility(View.GONE);
				channelListDbTask.addChoice(ncb);
			}
		}*/
		tv.setText(ncb.getCnname());
		tv.setTag(position);
		return convertView;
	}

	public void reorderItems(int oldPosition, int newPosition) {
		NewsChannelBean temp = list.get(oldPosition);
		if (oldPosition < newPosition) {
			for (int i = oldPosition; i < newPosition; i++) {
				Collections.swap(list, i, i + 1);
			}
		} else if (oldPosition > newPosition) {
			for (int i = oldPosition; i > newPosition; i--) {
				Collections.swap(list, i, i - 1);
			}
		}
		list.set(newPosition, temp);
	}

	public void setHideItem(int hidePosition) {
		this.mHidePosition = hidePosition;
		notifyDataSetChanged();
	}


	public void reset() {
		notifyDataSetChanged();
		/*
		resetList1 = new ArrayList<>();
		resetList2 = new ArrayList<>();
		for (NewsChannelBean ncb : list) {
			if (channelListDbTask.isChoice(ncb.getTid()) && !"0".equals(ncb.getTid())) {
				resetList1.add(ncb);
			} else {
				resetList2.add(ncb);
			}
		}
		// list.clear();
		list = resetList1;
		list.addAll(resetList2);
		notifyDataSetChanged();*/
	}

	@Override
	public View getMyView(int position, View convertView, ViewGroup parent) {

		return null;
	}

}
